package com.example.project.Function;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;

import com.example.project.Function.user.alarm;
import com.example.project.Function.user.contact;
import com.example.project.MainActivity;
import com.example.project.R;
import com.example.project.key.object.employee.employee;
import com.example.project.key.service.employeeService.employeeService;

public class mainpage extends AppCompatActivity {
    private DrawerLayout dl;
    private LinearLayout ll;
    private ConstraintLayout ct;
    private ConstraintLayout alarm;
    private contact ct1;
    private com.example.project.Function.user.alarm al;
    private LinearLayout btn_logout;
    private TextView show_name;

    private int TAG=0;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.drawer);
        dl=findViewById(R.id.drawer);
        ll=findViewById(R.id.linearlayout);
        btn_logout=findViewById(R.id.quit);
        show_name=findViewById(R.id.show_name);
        employeeService ems=new employeeService();
        show_name.setText(ems.getInfo(mainpage.this).getUsername()+",欢迎回来");

        btn_logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                employee e=new employee();
                e.setNull();
                employeeService ems =new employeeService();
                ems.saveUserInfo(mainpage.this,e);
                Intent quit =new Intent(mainpage.this, MainActivity.class);
                startActivity(quit);
            }
        });

        ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                        dl.openDrawer(GravityCompat.START);
            }
        });
        dl.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
                ll.setX(slideOffset*drawerView.getWidth());
            }

            @Override
            public void onDrawerOpened(@NonNull View drawerView) {
            }

            @Override
            public void onDrawerClosed(@NonNull View drawerView) {
            }

            @Override
            public void onDrawerStateChanged(int newState) {
            }
        });
                initView();
        ct1=new contact();
        al=new alarm();

    }

    private void initView()
    {
        ct=findViewById(R.id.contact);
        alarm=findViewById(R.id.alarm);
        ct.setOnClickListener(this::onClick);
        alarm.setOnClickListener(this::onClick);

    }

    public void onClick(View v){
        if(TAG==0)
        {
            TAG=1;
            switch (v.getId())
            {
                case R.id.contact:
                    getSupportFragmentManager().beginTransaction().add(R.id.fl_container,ct1).commitAllowingStateLoss();
                    break;
                case R.id.alarm:
                    getSupportFragmentManager().beginTransaction().add(R.id.fl_container,al).commitAllowingStateLoss();
                    break;
                default:
                    break;
            }
        }else {
            switch (v.getId())
            {
                case R.id.contact:
                    getSupportFragmentManager().beginTransaction().replace(R.id.fl_container,ct1).commitAllowingStateLoss();
                    break;
                case R.id.alarm:
                    getSupportFragmentManager().beginTransaction().replace(R.id.fl_container,al).commitAllowingStateLoss();
                    break;
                default:
                    break;
            }
        }

    }

}
